package com.example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    private final WebsocketHandler webSocketMessageHandler;

    public WebSocketConfig(WebsocketHandler websocketHandler) {
        this.webSocketMessageHandler = websocketHandler;
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 配置 WebSocket 的端点
        registry.addHandler(webSocketMessageHandler, "/ws/message")
                .setAllowedOrigins("*"); // 允许跨域
    }
}
